package com.hz.blogService.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.blogService.domain.Blog;
import com.hz.blogService.enums.IsAnonymousEnum;
import com.hz.blogService.mapper.UserMapper;
import com.hz.blogService.service.BlogService;
import com.hz.blogService.mapper.BlogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
* @author li
* @description 针对表【blogs(博客表)】的数据库操作Service实现
* @createDate 2023-09-23 17:15:33
*/
@Service
@RequiredArgsConstructor
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    private final BlogMapper blogMapper;
    private final UserMapper userMapper;


    @Override
    public int addBlog(Blog blog) {
        /* 设置发布博客信息 */
        Blog blogDB = new Blog();
        blogDB.setTitle(blog.getTitle());
        // 表情过滤：去除文章内容中的表情
        blogDB.setContent(EmojiParser.parseToAliases(blog.getContent()));
        blogDB.setAuthorId(blog.getAuthorId());
        blogDB.setAuthorUsername(blog.getAuthorUsername());
        blogDB.setPublishTime(new Date());
        // 设置匿名
        if (blog.getIsAnonymous() != 0)
            blogDB.setIsAnonymous(IsAnonymousEnum.YES_ANONYMOUS.getIsAnonymous());

        // 执行插入操作
        blogMapper.insert(blogDB);

        // 发布博客并返回新增后该记录的主键自增ID
        return blogDB.getId();
    }

    @Override
    // @Cacheable(value = "blogAll")  // 将结果存储到Redis缓存
    public String findBlogAll() {
        List<Blog> blogLIst = blogMapper.selectList(null);

        return JSON.toJSONString(blogLIst);
    }


    @Override
    @Cacheable(value = "blog_", key = "#id")  // 将结果存储到Redis缓存
    public String findBlogById(Integer id) {
        // 查询id=1的User时FastJSON提示User对象无法转换为JSON字符串（其他id没问题）
        return JSON.toJSONString(blogMapper.selectById(id));
    }


    @Override
    @CacheEvict(value = "blog_", key = "#id")  // 删除该博客Redis缓存
    public int deleteBlogById(Integer id) {
        return blogMapper.deleteById(id);
    }


    @Override
    @CachePut(value = "blog_", key = "#blog.id")  // 更新指定ID博客存储到Redis的缓存
    public int updateBlogById(Blog blog) {
        // 更新修改时间
        blog.setModifiedTime(new Date());

        // 更新用户修改信息并返回修改结果
        return blogMapper.updateById(blog);
    }


    @Override
    @Cacheable(value = "blogByKeyWords_", key = "#keywords")  // 将结果存储到Redis缓存
    public String findBlogByKeyWords(String keywords) {
        LambdaQueryWrapper<Blog> lqWrapper = new LambdaQueryWrapper<>();

        // 使用LambdaQueryWrapper构造条件（避免使用字符串表示字段，防止运行时错误）
        lqWrapper
                .like(StringUtils.isNotBlank(keywords), Blog::getTitle, keywords)
                .or()
                .like(StringUtils.isNotBlank(keywords), Blog::getContent, keywords);

        // 设置参数为LambdaQueryWrapper进行条件查询，返回查询结果
        return JSON.toJSONString(blogMapper.selectList(lqWrapper));
    }

    @Override
    public String findBlogAuthorById(Integer id) {
        Blog blog = blogMapper.selectById(id);

        return JSON.toJSONString(userMapper.selectUserById(blog.getAuthorId()));
    }
}




